/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.poligran.polla.mundialista.ingesoft.impl;

import co.com.poligran.polla.mundialista.ingesoft.base.Usuario;
import co.com.poligran.polla.mundialista.ingesoft.db.Entitie;
import co.com.poligran.polla.mundialista.ingesoft.dao.UsuarioDAO;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author alejo
 */
public class UsuarioImpl extends Entitie implements UsuarioDAO {

    @Override
    public Usuario GetByLogin(String usuario, String contrasenia) {
        try {
            this.query = "EXEC sp_GetUsuarioContrasenia_2 ?,?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.cs.setString(1, usuario);
            this.cs.setString(2, contrasenia);
            this.rs = cs.executeQuery();
            if (rs.next()) {
                return new Usuario();
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return null;
    }

    @Override
    public boolean Login(String usuario, String contrasenia) {
        return GetByLogin(usuario, contrasenia) != null;
    }

  //  public static void main(String[] args) {
  //      UsuarioDAO dAO = new UsuarioImpl();
 //       Usuario u = new Usuario(2, "1010541", "Update test", "M77edddina", "JuRod77dmt", "ggjussjju@poli.edu.co", "1234", new Date(System.currentTimeMillis()), "10");
//        System.out.println(dAO.insert(u));
 //       for (Usuario un : dAO.selectAll()) {
  //          System.out.println(un);
  //      }
   //     System.out.println("Login: " + dAO.Login("gg@elearn.edu.mx", "1234"));
   //     System.out.println("Existe: " + dAO.select(4));
    //    System.out.println("Delete: " + dAO.delete(10));
     //   System.out.println("Update: " + dAO.update(u));

  //  }

    @Override
    public boolean insert(Usuario e) {
        boolean inserto = false;
        try {
            this.cs = conn.prepareCall("EXEC sp_InsertUsuario ?,?,?,?,?,?");

            this.cs.setString(1, e.getIdentificacion());
            this.cs.setString(2, e.getNombre());
            this.cs.setString(3, e.getApellido());
            this.cs.setString(4, e.getNombreUsuario());
            this.cs.setString(5, e.getCorreoElectronico());
            this.cs.setString(6, e.getContrasena());

            this.cs.executeUpdate();

            inserto = true;

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return inserto;
    }

    @Override
    public Usuario select(int idEntitie) {
        Usuario selectUsuario = null;
        try {
            this.query = "EXEC sp_GetUsuario ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.cs.setInt(1, idEntitie);
            this.rs = cs.executeQuery();
            if (rs.next()) {
                selectUsuario = new Usuario();
                selectUsuario.setIdUsuario(rs.getInt(1));
                selectUsuario.setIdentificacion(rs.getString(2));
                selectUsuario.setNombre(rs.getString(3));
                selectUsuario.setApellido(rs.getString(4));
                selectUsuario.setNombreUsuario(rs.getString(5));
                selectUsuario.setCorreoElectronico(rs.getString(6));
                selectUsuario.setContrasena(rs.getString(7));
                selectUsuario.setFecCreacion(rs.getDate(8));
                selectUsuario.setNumCuentaPSE(rs.getString(9));
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectUsuario;
    }

    @Override
    public LinkedList<Usuario> selectAll() {
        LinkedList<Usuario> selectUsuarios = null;
        try {
            this.query = "select * from usuario";
            this.ps = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.rs = ps.executeQuery();
            selectUsuarios = new LinkedList();
            while (rs.next()) {
                Usuario usuario = new Usuario();
                usuario.setIdUsuario(rs.getInt(1));
                usuario.setIdentificacion(rs.getString(2));
                usuario.setNombre(rs.getString(3));
                usuario.setApellido(rs.getString(4));
                usuario.setNombreUsuario(rs.getString(5));
                usuario.setCorreoElectronico(rs.getString(6));
                usuario.setContrasena(rs.getString(7));
                usuario.setFecCreacion(rs.getDate(8));
                usuario.setNumCuentaPSE(rs.getString(9));
                selectUsuarios.add(usuario);
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectUsuarios;
    }

    @Override
    public boolean update(Usuario e) {
        boolean update = false;
        try {
            this.cs = conn.prepareCall("EXEC sp_UpdateUsuario ?,?,?,?,?,?");

            this.cs.setString(1, e.getIdentificacion());
            this.cs.setString(2, e.getNombre());
            this.cs.setString(3, e.getApellido());
            this.cs.setString(4, e.getNombreUsuario());
            this.cs.setString(5, e.getCorreoElectronico());
            this.cs.setString(6, e.getContrasena());

            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                update = true;
            }

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return update;
    }

    @Override
    public boolean delete(int idEntitie) {
        boolean resp = false;
        try {
            this.query = "EXEC sp_DeleteUsuario ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            // set preparedstatement 
            this.cs.setInt(1, idEntitie);
            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                resp = true;
            }
        } catch (SQLException se) {
            System.out.print("exepction  " + se.getMessage());
        } finally {
            try {
                this.cs.close();
            } catch (SQLException ex) {
                System.out.print("exepction  " + ex.getMessage());
            }
        }
        return resp;
    }

}
